package org.sun.sample.pojo;

import org.apache.zookeeper.*;

/**
 * 使用ZooKeeper
 */
public class ZooKeeperSample implements Runnable{

    private ZooKeeper zk;

    public ZooKeeperSample() {
        linkZooKeeper();
    }

    protected void linkZooKeeper() {
        try {
            zk = new ZooKeeper("127.0.0.1:2181", 999999999, new Watcher() {
                public void process(WatchedEvent watchedEvent) {
                    System.out.println(watchedEvent.toString());
                }
            });
            zk.exists("/root", true);
            zk.exists("/root/alone", true);
            zk.create("/root/alone", "Yes I do !".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }

    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (Exception exp) {
                exp.printStackTrace();
            }
        }
    }

}
